home *** CD-ROM | disk | FTP | other *** search
- /*
- DemoNetAccess.cpp
-
- Demo cross-platform web access class for MacHack 97
-
- Al Evans
-
- 6/14/97
-
- */
-
- #include <string.h>
- #include "DemoNetAccess.h"
-
- // Uses a "visible" public class to access a "hidden" implementation class
- // The implementation is completely different for the two platforms
-
- #if TARGET_IS_MACOS
- // Mac implementation uses Internet Config
- #include "ICAPI.h"
- #include "ICKeys.h"
- #include "ICTypes.h"
- #endif
-
- // Private implementation class
- class DemoWebImpl {
-
- DemoWebImpl();
-
- ~DemoWebImpl();
-
- long GoToWebSite(char const *url);
-
- static void ShowFailedAlert();
-
- friend struct DemoWebConnection;
-
- #ifdef TARGET_IS_MACOS
- ICInstance icRef;
- #endif
- };
-
- // There will be a single instance of the implementation class
- static DemoWebImpl *sWebImpl = nil;
-
- #if 0
- #pragma mark ---------------------------------
- #pragma mark | Macintosh Implementation
- #pragma mark ---------------------------------
- #endif
-
-
- #if TARGET_IS_MACOS
- DemoWebImpl::DemoWebImpl()
- {
- // Open Internet Config
- ICError ret = ICStart(&icRef, 'DEMO');
-
- // Use default IC preferences
- if (ret == noErr)
- ret = ICFindConfigFile(icRef, 0, nil);
-
- // A real implementation should check for the presence and accessibility
- // of a "preferred" web browser.
-
- if (ret != noErr)
- throw(ret);
- }
-
- DemoWebImpl::~DemoWebImpl()
- {
- ICError ret = ICStop(icRef);
- }
-
- long DemoWebImpl::GoToWebSite(char const *url)
- {
- long start = 0;
- long length = strlen(url);
- return ICLaunchURL(icRef, "\phttp", (char *) url, length, &start, &length);
- }
-
- void DemoWebImpl::ShowFailedAlert()
- {
- // Not implemented in this demo
- SysBeep(1);
- }
- #endif
-
- #if 0
- #pragma mark ---------------------------------
- #pragma mark | Windows 95 Implementation
- #pragma mark ---------------------------------
- #endif
-
- #if TARGET_IS_WIN95
-
- DemoWebImpl::DemoWebImpl()
- {
- // A real implementation should check for the presence and accessibility
- // of a "preferred" web browser.
- }
-
-
- DemoWebImpl::~DemoWebImpl()
- {
- }
-
- long DemoWebImpl::GoToWebSite(char const *url)
- {
- HINSTANCE browserInstance = ShellExecute(NULL, "open", (char *) url,
- NULL, NULL, SW_SHOWNORMAL);
- if ((long) browserInstance <= 32)
- return ((long) browserInstance);
- else
- return 0;
-
- }
-
- void DemoWebImpl::ShowFailedAlert()
- {
- // Not implemented in this demo
- }
-
- #endif
-
- #if 0
- #pragma mark ---------------------------------
- #pragma mark | Public Interface
- #pragma mark ---------------------------------
- #endif
-
- DemoWebConnection::DemoWebConnection()
- {
- if (sWebImpl == nil) {
- try {
- sWebImpl = new DemoWebImpl();
- }
- catch (...) {
- sWebImpl = nil;
- }
- }
- }
-
- DemoWebConnection::~DemoWebConnection()
- {
- if (sWebImpl) delete sWebImpl;
- sWebImpl = nil;
- }
-
- Boolean DemoWebConnection::CanAccessWeb()
- {
- return (sWebImpl != nil);
- }
-
- long DemoWebConnection::OpenSite(char const *url)
- {
- long result = -1;
- if (sWebImpl)
- result = sWebImpl->GoToWebSite(url);
- if (result != 0)
- DemoWebImpl::ShowFailedAlert();
- return result;
- }
-